<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">
        当前位置：首页
      </el-breadcrumb-item>
      <el-breadcrumb-item>供应商管理</el-breadcrumb-item>
      <el-breadcrumb-item>供应商配置</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索区 -->
      <el-form
        ref="searchFormRef"
        :inline="true"
        :model="queryInfo.query"
        size="mini"
      >
        <el-form-item label="关键字" prop="keyword">
          <el-input
            v-model="queryInfo.query.keyword"
            placeholder="请输入供应商全称、简称或ID"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            class="theme-background-color"
            size="mini"
            @click="searchList"
          >
            搜索
          </el-button>
          <el-button size="mini" @click="resetSearchForm"> 重置 </el-button>
        </el-form-item>
      </el-form>

      <!-- 操作区域 -->
      <div>
        <el-button
          class="theme-background-color"
          icon="el-icon-plus"
          size="mini"
          @click="$router.push('/supplier/supplierAdd')"
        >
          新增供应商
        </el-button>
      </div>

      <!-- 列表区域 -->
      <el-table :data="list" border stripe>
        <!-- <el-table-column label="序号" type="index"></el-table-column> -->
        <el-table-column label="供应商简称【ID】">
          <template slot-scope="scope"
            >{{ scope.row.short_name }}【{{ scope.row.id }}】</template
          >
        </el-table-column>
        <el-table-column label="供应商全称" prop="full_name"></el-table-column>
        <el-table-column
          label="供应商类型"
          prop="provider_type.name"
        ></el-table-column>
        <el-table-column label="供应产品" prop="describe"></el-table-column>
        <el-table-column
          label="结算方式"
          prop="clear_type.name"
        ></el-table-column>
        <el-table-column
          label="对接方式"
          prop="docking_mode.name"
        ></el-table-column>
        <el-table-column label="操作" width="100px">
          <template slot-scope="scope">
            <el-link
              type="primary"
              @click="$router.push('/supplier/supplierEdit?id=' + scope.row.id)"
            >
              编辑
            </el-link>
            &nbsp;
            <el-link type="danger" @click="handleDeleteSupplier(scope.row)">
              删除
            </el-link>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        query: {
          keyword: ''
        },
        pagenum: 1,
        pagesize: 20
      },
      list: [],
      total: 0
    }
  },
  created() {
    this.getListData()
  },
  methods: {
    async getListData() {
      // 返回数据重命名为res
      const { data: res } = await this.$http.post(
        '/admin/provider/list',
        this.queryInfo
      )
      // console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.list = res.data.list
      this.total = res.data.total
    },
    searchList() {
      this.queryInfo.pagenum = 1
      this.getListData()
    },
    resetSearchForm() {
      this.$refs.searchFormRef.resetFields()
      this.getListData()
    },
    // 监听 pagesize 改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getListData()
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getListData()
    },
    // 删除供应商
    async handleDeleteSupplier(row) {
      // 弹框询问是否删除数据
      const confirmResult = await this.$confirm(
        '此操作将删除该供应商【' + row.short_name + '】, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch((err) => err)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      const { data: res } = await this.$http.post('/admin/provider/remove', {
        id: row.id
      })
      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg)
      }
      this.$message.success(res.meta.msg)
      this.getListData()
    }
  }
}
</script>

<style lang="less" scoped>
</style>
